package com.ggy.babybed.socketwork;

import android.util.Log;

import java.io.IOException;
import java.net.Socket;

/**
 * @author gonggy
 *
 */
public class NetworkFactory {
    public static final String TAG = "NetworkFactory";

    /**
     *
     * @param host
     * @param port
     * @param timeout
     * @return
     * @throws IOException
     */
    public static Socket createSocket(String host, int port, int timeout) throws IOException{
        if(port <= 0 || port > 65535){
            Log.e(TAG, "端口非法");
        }
        if(timeout < 0){
            Log.e(TAG, "超时时间非法");
        }

        Socket socket = null;
        try {
            socket = new Socket(host, port);
            socket.setSoTimeout(timeout);
        } catch (IOException e) {
            Log.e(TAG, "连接失败,详细信息："+e.getMessage());
            createSocket();
        }
        return socket;
    }

    public static Socket createSocket() throws IOException{
        Socket socket = new Socket(NetworkConstant.DEFAULT_HOST, NetworkConstant.DEFAULT_PORT);
        socket.setSoTimeout(NetworkConstant.DEFAULT_TIMEOUT);
        return socket;
    }
}

